/**
 * 
 */
package com.google.code.cisx.gwt.client.layouts;

import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.client.ui.FlexTable.FlexCellFormatter;

/**
 * @author <a href='mailto:rahul.thakur.xdev@gmail.com'>Rahul Thakur</a>
 * @version $Id: MainLayoutTemplate.java 260 2008-11-30 09:06:35Z rahul.thakur.xdev $
 * @since 1.0
 */
public class MainLayoutTemplate extends LayoutTemplate {

  public static final String PLACEHOLDER_HEADER = "header";

  public static final String PLACEHOLDER_FOOTER = "footer";

  public static final String PLACEHOLDER_NAVIGATION = "navigation";

  public static final String PLACEHOLDER_CONTENT = "content";

  /**
   * Content to set in page header.
   */
  private Widget header;

  /**
   * Content to set in page footer.
   */
  private Widget footer;

  /**
   * Content to set in page navigation.
   */
  private Widget navigation;

  /**
   * Content to set in page's content area.
   */
  private Widget content;

  private VerticalPanel panel = new VerticalPanel();

  FlexTable flextable = new FlexTable();

  /**
   * 
   */
  public MainLayoutTemplate() {
    super();
  }

  /**
   * {@inheritDoc}
   */
  public void render() {
    initWidget(panel);

    // build layout.
    // force layou on flextable
    flextable.setWidget(0, 0, header);
    flextable.setWidget(1, 0, navigation);
    flextable.setWidget(1, 1, content);
    flextable.setWidget(2, 0, footer);

    // set colspans
    FlexCellFormatter cellFormatter = flextable.getFlexCellFormatter();
    cellFormatter.setColSpan(0, 0, 3); // header spans 3 cols
    cellFormatter.setColSpan(1, 1, 2); // content spans 3 cols
    cellFormatter.setColSpan(2, 0, 3); // footer spans 3 cols

    cellFormatter.setHeight(0, 0, "10%"); // height of header
    // let the middle row occupy the remaining space
    cellFormatter.setHeight(2, 0, "5%"); // height of footer

    flextable.getColumnFormatter().setWidth(0, "30%");
    flextable.getColumnFormatter().setWidth(2, "10%");
    flextable.setBorderWidth(1);

    panel.add(flextable);
    panel.setHeight("100%");
    panel.setWidth("100%");

    flextable.setHeight("100%");
    flextable.setWidth("100%");
    flextable.setCellPadding(2);
  }

  /**
   * {@inheritDoc}
   */
  // @Override
  public Widget getComponent(String placeholderId) {
    if (placeholderId.equals(PLACEHOLDER_HEADER))
      return this.header;
    else if (placeholderId.equals(PLACEHOLDER_FOOTER))
      return this.footer;
    else if (placeholderId.equals(PLACEHOLDER_NAVIGATION))
      return this.navigation;
    else if (placeholderId.equals(PLACEHOLDER_CONTENT))
      return this.content;
    // else null
    return null;
  }

  /**
   * {@inheritDoc}
   */
  // @Override
  public LayoutTemplate setComponent(String placeholderId, Widget component) {
    if (placeholderId.equals(PLACEHOLDER_HEADER))
      this.header = component;
    else if (placeholderId.equals(PLACEHOLDER_FOOTER))
      this.footer = component;
    else if (placeholderId.equals(PLACEHOLDER_NAVIGATION))
      this.navigation = component;
    else if (placeholderId.equals(PLACEHOLDER_CONTENT))
      this.content = component;
    return this;
  }

}
